public class 二叉树的最小深度 {
    //https://leetcode.cn/problems/minimum-depth-of-binary-tree/
    public int minDepth(TreeNode root) {
        if(root == null){
            //空树
            return 0;
        }
        if(root.left == null && root.right == null){
            //叶子节点
            return 1;
        }
        //每个设成最大值, 如果是空树就不会进入递归, 比较的时候不需要再判断
        int minLeft = Integer.MAX_VALUE;
        int minRight = Integer.MAX_VALUE;
        if(root.left != null){
            minLeft = minDepth(root.left);

        }
        if(root.right != null){
            minRight = minDepth(root.right);
        }
        //取最小值并加上这一层
        return Math.min(minLeft,minRight) + 1;
    }
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        // 无参构造函数
        TreeNode() {}

        // 只带一个值的构造函数
        TreeNode(int val) {
            this.val = val;
        }

        // 带值、左子节点和右子节点的构造函数
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
